package com.xs.springagain2.mysql;

import com.xs.springagain2.config.DataSourceConfig;
import com.xs.springagain2.pojo.Library;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class TestBean {

    @Test
    public void test1() throws SQLException {
        AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(DataSourceConfig.class);
        DataSource dataSource = (DataSource) ioc.getBean("dataSource");
        Connection connection = dataSource.getConnection();

        System.out.println( connection);

        JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);
//        String addSql = "insert into library (title,number,momey) values (?,?,?)";
//        jdbcTemplate.update(addSql, "springAgain", 99, 100);

        String querySql = "select * from library";
        List query = jdbcTemplate.query(querySql, new BeanPropertyRowMapper(Library.class));
        System.out.println( query);
    }
}
